package com.bf.electroplating;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;

@SpringBootApplication
@EnableScheduling
@Slf4j
@MapperScan (basePackages = {"com.bf.electroplating.mapper"})
@EnableConfigurationProperties
public class ElectroplatingApplication {

	public static void main(String[] args) throws Exception {
		String path = getRootPath();
		log.info("[DataAnalystApplication -- start]外部配置地址：{}", path);
		String filePath = path + File.separator + "application.properties";
		log.info("[filePath 配置地址：{}", filePath);
		if (!new File(filePath).exists()) {
			ClassPathResource resource = new ClassPathResource("application.properties");
			if (args.length < 1) {
				log.info("[DataAnalystApplication -- start]请输入启动环境：dev、test、pro");
				return;
			}
			if (!"dev".equals(args[0]) && !"test".equals(args[0]) && !"pro".equals(args[0])) {
				log.info("[DataAnalystApplication -- start]输入环境错误：{}", args[0]);
				return;
			}
			log.info("[DataAnalystApplication -- start]加载配置文件：{}", resource.getPath());
			InputStream ism = resource.getInputStream();
			Properties properties = new Properties();
			properties.load(ism);
			properties.setProperty("spring.profiles.active", args[0]);
			SpringApplication app = new SpringApplication(ElectroplatingApplication.class);
			app.setDefaultProperties(properties);
			app.run(args);
		} else {
			log.info("[DataAnalystApplication -- start]加载配置文件：{}", filePath);
			InputStream ism = new FileInputStream (filePath);
			Properties properties = new Properties();
			properties.load(ism);
			SpringApplication app = new SpringApplication(ElectroplatingApplication.class);
			app.setDefaultProperties(properties);
			app.run(args);
			//		}
		}

		//	@Bean
		//	public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
		//		return factory -> {
		//			ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
		//			factory.addErrorPages(error404Page);
		//		};
		//	}
	}

	private static String getRootPath() {
		URL url=ElectroplatingApplication.class.getProtectionDomain().getCodeSource().getLocation();
		String filePath=null;
		try {
			filePath= URLDecoder.decode(url.getPath(),"utf-8");
		}catch (Exception e){
			log.error("获取路径信息异常");
		}
		log.info ( "path1"+filePath );
		filePath=filePath.split ( ".jar" )[0];
		log.info ( "path2"+filePath );
//		if (filePath.endsWith(".jar")){
			filePath=filePath.substring(0,filePath.lastIndexOf("/")+1);
//		}
//		File file=new File(filePath);
//		filePath=file.getAbsolutePath();
		log.info ( "path3"+filePath );
//		filePath=filePath.split("file:")[0];
		if (filePath.startsWith("nested:")) {
			filePath = filePath.substring("nested:".length());
		}
		log.info("jar包所在目录:"+filePath);
		return filePath;
	}
}
